Component({
  data: {
    showHistory:true,
    searchText:'',
    hList:[],
    hotList:['服务员','营业员','收银员','厨师', '零售员']
  },


  lifetimes:{
    attached(){
      const that = this;
      wx.getStorage({
        key:"searchHlist",
        success(res){
          const result = res.data;
          that.setData({
            hList:result
          })
        }
      })
    }
  },

  methods:{
    searchText(e){
      const searchValue = e.detail.value;
      this.setData({
        searchText:searchValue
      })
    },
  
    clearSerchText(){
      this.setData({
        searchText:''
      })
    },
  
    searchStart(e){
      const value = e.detail.value;
      const hList = this.data.hList;

      this.setData({
        showHistory:false
      })
      this.searchList(value);
      
      if(hList.length>5){
        for(let item of hList){
          if(item == value){
            return false;
          }
        }
        hList.pop();
        hList.unshift(value);
      }else{
        for(let item of hList){
          if(item == value){
            return false;
          }
        }
        hList.unshift(value);
      }
      this.setData({
        hList:hList,
      })
      wx.setStorageSync('searchHlist', hList);
    },
  
    deleteHlist(){
      this.setData({
        hList:[]
      })
      wx.setStorageSync('searchHlist', this.data.hList);
    },

    keywordsClick(e){
      const tag = e.currentTarget.dataset.tag;
      this.setData({
        searchText:tag,
        showHistory:false
      })
      this.searchList(tag);
    },

    clickCancel(){
      this.setData({
        showHistory:true,
        searchText:""
      })
      this.triggerEvent("search",{
        showList:false
      })
    },

    searchList(val){
      this.triggerEvent("search",{
        searchText:val,
        showList:true
      })
    }
  }
})
